<!--  -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
     <a-table size="small" :columns="columns" :data-source="dataList" @expand="expand"  :expandedRowKeys="expandedRowKeys" class="components-table-demo-nested">
         
         <a-avatar  slot="imageUrl" slot-scope="imageUrl" :src="imageUrl" />
          <div slot="status" slot-scope="status">
                <a-switch checked-children="上架" un-checked-children="下架" disabled :checked="status == 1 ? true : false" />
            </div>
          <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        <a-table
        slot="expandedRowRender"
        slot-scope="text"
        :columns="columns"
        :data-source="innerData"
        :pagination="false"
        >
         <a-avatar  slot="imageUrl" slot-scope="imageUrl" :src="imageUrl" />
            <div slot="status" slot-scope="status">
                <a-switch checked-children="上架" un-checked-children="下架" disabled :checked="status == 1 ? true : false" />
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>
    </a-table>
        <!-- 添加、修改、删除个人推送消息 -->
        <a-modal
            title="添加、修改分类"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                     <a-form-item label="分类名称">
                        <a-input v-model="item.cateName" placeholder="请输入分类名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="分类层级">
                         <a-select v-model="item.level"   style="width: 120px" >
                                <a-select-option v-for="(item,index) in levelList" :value="item.id" :key="item.id">
                                    {{item.name}}
                                </a-select-option>
                         </a-select>       
                     </a-form-item>
                     <a-form-item label="主分类" v-if="item.level==2" >
                         <a-select v-model="item.parentId"   style="width: 120px">
                                <a-select-option v-for="(item,index) in leveClassify" :value="item.id" :key="item.id">
                                    {{item.cateName}}
                                </a-select-option>
                         </a-select>       
                     </a-form-item>
                      <a-form-item label="图片">
                        <a-tag color="#f50">
                            修改时，如果上传图片就会覆盖掉之前的图片，如果不修改就无需上传
                        </a-tag>
                        <div class="clearfix">
                            <a-upload
                            list-type="picture-card"
                            :file-list="fileList"
                            
                            @change="handleChange"
                            >
                            <div v-if="fileList.length < 1">
                                <a-icon type="plus" />
                                <div class="ant-upload-text">
                                    选择图片
                                </div>
                            </div>
                            </a-upload>
                        </div>
                    </a-form-item>
                     <a-form-item label="是否上架">
                        <a-radio-group v-model="item.status" >
                            <a-radio  v-for="(item,i) in statusList" :key="i" :value="item.id">{{item.name}}</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryClassify , updateClassify } from '@/api/mall/classify';
import { queryBrand }  from '@/api/brand';
export default {
    data () {
        return {
            userId: undefined,
            brandId: this.global.brandId,
            brandList: [],
            isload: false,
            dataList: [],
            size: 10,
            page: 1,
            total: 0,
            columns,
            visible: false,
            confirmLoading: false,
            item: {
                id: undefined,
                level : '1',
                parentId : '0',
                status : '0',
                cateName : ''
            },
             statusList: [
                { id: '1', name: '是'},
                { id: '0', name: '否'},
            ],
             levelList: [
                { id: '1', name: '一级分类'},
                { id: '2', name: '二级分类'},
            ],
            isAdd: true,
            innerData:[],
            expandedRowKeys:[],
             fileList: [],
            leveClassify:[]//  分类添加
        };
    },
    async created(){
        this.getData()
        
    },
    methods: {
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'brandId':_this.brandId,
                'parentId':'0',
                'size':_this.size,
                'page':_this.page - 1,
            }
            _this._queryClassify(params,1)
        },
        _queryClassify(params,type){
            var _this=this
            queryClassify(params)
            .then(res=>{
                _this.isload = false
                if(type==1){
                    _this.dataList=[]
                }
                 
                if(res.success){
                    if(type==1){
                         res.data.content.forEach((item,index) => {
                            item.index=index
                            _this.dataList.push(item)
                        });
                        _this.total = res.data.totalElements
                    }else if(type==2){
                         _this.innerData=res.data.content
                    }else if(type==3){
                         _this.leveClassify=res.data.content
                    }
                   
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 查询下级分类
        expand(expanded, record) {
                 let temp = []
                    if (expanded) {
                        temp.push(record.index)
                    }
                  this.expandedRowKeys=temp
                   const params = {
                        'brandId':this.brandId,
                        'parentId':record.id,
                        'size':100,
                        'page':0,
                    }
                   this._queryClassify(params,2)

        },

        // 添加
        add(){
            this.isAdd = true
            this.item={
                id : '',
                level :'1',
                parentId : '0',
                status : '0',
                cateName :''
            }
            this.fileList = []
            this.visible = true
             this.levelChange()
        },
        // 修改
        update(item){
            this.isAdd = false
            this.item = JSON.parse(JSON.stringify(item))
              this.fileList = []
            this.visible = true
           this.levelChange()
            
        },
        levelChange(){
             const params = {
                'brandId':this.brandId,
                'parentId':0,
                'size':100,
                'page':0,
                }
         this._queryClassify(params,3)
        },
        // 选择图片
        handleChange({ fileList }) {
            this.fileList = fileList;
        },
        // 确认添加、修改
        handleOk(){
             let _this = this
            if(_this.item.cateName == ''){
                _this.$message.warning('请输入分类名称！');
                return
            }
           
            var formData = new FormData()
            formData.append('brandId',_this.brandId)
            formData.append('id',_this.item.id)
            formData.append('status',_this.item.status)
            formData.append('cateName',_this.item.cateName)
            formData.append('level',_this.item.level)
            if(_this.item.level==1){
                 formData.append('parentId',0)
            }else{
                 formData.append('parentId',_this.item.parentId)
            }
            _this.fileList.forEach(item=>{
                formData.append('file',item.originFileObj)
            })
            // formData.append('isFile',true)
            updateClassify(formData)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.isAdd ? '添加成功' : '修改成功',
                        duration: 8,
                    })
                    _this.getData()
                    _this.visible = false
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除消息
        del(item){
            let _this = this
              var formData = new FormData()
                formData.append('brandId',item.brandId)
                formData.append('id',item.id)
                formData.append('isDelete',1)
           
            updateClassify(formData)
            .then(res=>{
                if(res.success){
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                    _this.getData()
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>